Internet Explorer box model bug

The Internet Explorer box model bug refers to the way in which earlier versions of Internet Explorer handle the sizing of elements in a web page, which differs from the standard way recommended by the W3C for the Cascading Style Sheets language. As of Internet Explorer 6, the browser supports an alternative rendering mode (called the "standards-compliant mode") which solves this discrepancy. However, for backward compatibility reasons, all versions still behave in the usual, non-standard way by default (see quirks mode). Internet Explorer for Mac is not affected by this non-standard behavior.

Contents

Background

The Cascading Style Sheets (CSS) specification describes how elements of web pages are displayed by graphical browsers. Section 4 of the CSS1 specification defines a "formatting model" that gives block-level elements—such as p and blockquote—a width and height, and three levels of boxes surrounding it: padding, borders, and margins.[1] While the specification never uses the term "box model" explicitly, the term has become widely used by web developers and web browser vendors.

Before HTML 4 and CSS, very few HTML elements supported both border and padding, so the definition of the width and height of an element was not very contentious. However, it varied depending on the element. The HTML width attribute of a table defined the width of the table including its border.[2] On the other hand, the HTML width attribute of an image defined the width of the image itself (inside any border).[3] The only element to support padding in those early days was the table cell. Width for the cell was defined as "the suggested width for a cell content in pixels excluding the cell padding."[4]

In 1996, CSS[5] introduced margin, border and padding for many more elements. It adopted a definition width in relation to content, border, margin and padding similar to that for a table cell.[6] This has since become known as the W3C box model.

At the time, very few browser vendors implemented the W3C box model to the letter. The two major browsers at the time, Netscape 4.0 and Internet Explorer 4.0 both defined width and height as the distance from border to border.[7] This has been referred to as the traditional[8] or the Internet Explorer box model.[9]

Bug

According to the CSS1 specification, released by the World Wide Web Consortium (W3C) in 1996 and revised in 1999, when a width or height is explicitly specified for any block-level element, it should determine only the width or height of the visible element, with the padding, borders, and margins applied afterward.[1][10] Internet Explorer in "quirks mode" includes the content, padding and borders within a specified width or height; this results in a narrower or shorter rendering of a box than it would result following the standard bahavior.[11]

Workarounds

Internet Explorer versions 6 and onward are not affected by the bug if the page contains certain HTML document type declarations. These versions maintain the buggy behavior when in quirks mode for reasons of backward compatibility.[12] For example, quirks mode is triggered:

Various workarounds have been devised to force Internet Explorer versions 5 and earlier to display Web pages using the W3C box model. These workarounds generally exploit unrelated bugs in Internet Explorer's CSS selector processing in order to hide certain rules from the browser. The best known of these workarounds is the “box model hack” developed by Tantek Çelik, a former Microsoft employee who developed this idea while working on Internet Explorer for the Macintosh. It involves specifying a width declaration for Internet Explorer for Windows, and then overriding it with another width declaration for CSS-compliant browsers. This second declaration is hidden from Internet Explorer for Windows by exploiting other bugs in the way that that browser parses CSS rules. The implementation of these CSS “hacks” has been further complicated by the public release of Internet Explorer 7, which has had some issues fixed, but not others, causing undesired results in pages using these hacks.[12]

Box model hacks have proven unreliable because they rely on bugs in browsers' CSS support that may be fixed in later versions. For this reason, some Web developers have instead recommended either avoiding specifying both width and padding for the same element or using conditional comment and/or CSS filters to work around the box model bug in older versions of Internet Explorer.[14][15]

Support for Internet Explorer's box model

Web designer Doug Bowman has said that the original Internet Explorer box model represents a better, more logical approach.[16] Peter-Paul Koch gives the example of a physical box, whose dimensions always refer to the box itself, including potential padding, but never its content.[8] He says that this box model is more useful for graphic designers, who create designs based on the visible width of boxes rather than the width of their content.[17] Bernie Zimmermann says that the Internet Explorer box model is closer to the definition of cell dimensions and padding used in the HTML table model.[18]

The W3C has included a "box-sizing" property in CSS3. When box-sizing: border-box; is specified for an element, any padding or border of the element is drawn inside the specified width and height, "as commonly implemented by legacy HTML user agents".[19] Internet Explorer 8, Opera 7.0 and later, and Konqueror 3.3.2 and later support the CSS3 box-sizing property. Gecko-based browsers such as Mozilla Firefox support the same functionality using a proprietary "-moz-box-sizing" property,[20] and WebKit browsers such as Apple Safari and Google Chrome support it as a proprietary "-webkit-box-sizing" property.[21]

References

  1. ^ a b Håkon Wium Lie; Bert Bos (11 January 1999). "Cascading Style Sheets, level 1". World Wide Web Consortium. http://www.w3.org/TR/1999/REC-CSS1-19990111#formatting-model. Retrieved 2007-06-24. 
  2. ^ Raggett, Dave (23 January 1996). "The HTML3 Table Model". World Wide Web Consortium. http://www.w3.org/TR/WD-tables-960123. Retrieved 27 July 210. "RFC 1942 HTML Tables". IETF. http://www.ietf.org/rfc/rfc1942.txt. "The default table width is the space between the current left and right margins." 
  3. ^ Raggett, Dave; Arnaud Le Hors, Ian Jacobs (24 December 1999). "13 Objects, Images, and Applets # Width and height". World Wide Web Consortium. http://www.w3.org/TR/1999/REC-html401-19991224/struct/objects.html#h-13.7.1. Retrieved 27 July 2010. 
  4. ^ Raggett, Dave (14 January 1997). "HTML 3.2 Reference Specification". World Wide Web Consortium. http://www.w3.org/TR/REC-html32. Retrieved 2010-07-07. 
  5. ^ Håkon Wium Lie; Bert Bos (Dec 1996). "Cascading Style Sheets, level 1". World Wide Web Consortium. http://www.w3.org/TR/CSS1/. Retrieved 27 July 2010. 
  6. ^ "Cascading Style Sheets, level 1". World Wide Web Consortium. http://www.w3.org/TR/REC-CSS1-961217. Retrieved 2010-07-07. 
  7. ^ "Box model tweaking". CSS2 tests. Xs4all.nl. http://www.xs4all.nl/~ppk/css2tests/box.html. Retrieved 2010-07-07. 
  8. ^ a b "CSS2 - Box model tweaking". Quirksmode.org. http://www.quirksmode.org/css/box.html. Retrieved 2010-07-07. 
  9. ^ Johansson, Roger (21 December 2006). "Internet Explorer and the CSS box model". http://www.456bereastreet.com/archive/200612/internet_explorer_and_the_css_box_model/. Retrieved 2008-11-12. 
  10. ^ Håkon Wium Lie; Bert Bos (17 December 1996). "Cascading Style Sheets, level 1". World Wide Web Consortium. http://www.w3.org/TR/REC-CSS1-961217#formatting-model. Retrieved 2006-12-09. 
  11. ^ Lance Silver (March 2001). "CSS Enhancements in Internet Explorer 6". Microsoft Developer Network. Microsoft. http://msdn2.microsoft.com/en-us/library/bb250395.aspx#cssenhancements_topic3. Retrieved 2007-06-24. 
  12. ^ a b Markus Mielke (26 September 2006). "Cascading Style Sheet Compatibility in Internet Explorer 7". Microsoft Developer Network. Microsoft. http://msdn2.microsoft.com/en-us/library/bb250496.aspx. Retrieved 2007-06-24.  "Pages authored under non-strict mode (quirks) will not change behavior in IE7 and will not be affected by broken CSS filters.Pages authored under non-strict mode (or “quirks mode”) will not change behavior in IE7."
  13. ^ "!DOCTYPE". Microsoft Developer Network. Microsoft. http://msdn.microsoft.com/en-us/library/ms535242(VS.85).aspx. Retrieved 2007-01-13.  "The following examples show how to use the !DOCTYPE declaration to specify the DTD a document conforms to, and to switch Internet Explorer 6 and later to standards-compliant mode."
  14. ^ Roger Johansson (21 December 2006). "Internet Explorer and the CSS box model". 456 Berea Street. http://www.456bereastreet.com/archive/200612/internet_explorer_and_the_css_box_model/. Retrieved 2007-01-14. 
  15. ^ Arve Bersvendsen (February 2004). "Hack-free CSS for IE". Arve Bersvendsen's weblog. http://virtuelvis.com/archives/2004/02/css-ie-only. Retrieved 2007-01-16. 
  16. ^ "Vorsprung durch Webstandards | Douglas Bowman declares his love to CSS". Vorsprungdurchwebstandards.de. http://www.vorsprungdurchwebstandards.de/interviews/fallinginlovewithcss/douglas-bowman/. Retrieved 2010-07-07. 
  17. ^ "Vorsprung durch Webstandards | Peter-Paul Koch declares his love to CSS". Vorsprungdurchwebstandards.de. http://www.vorsprungdurchwebstandards.de/interviews/fallinginlovewithcss/peter-paul-koch/. Retrieved 2010-07-07. 
  18. ^ "Box Model Enlightenment, Bernie Zimmermann". Bernzilla.com. 4 April 2003. http://www.bernzilla.com/item.php?id=33. Retrieved 2010-07-07. 
  19. ^ "CSS3 Basic User Interface Module". World Wide Web Consortium. http://www.w3.org/TR/css3-ui/#box-sizing. 
  20. ^ "-moz-box-sizing". Mozilla Developer Center. Mozilla. 11 April 2009. https://developer.mozilla.org/en/CSS/box-sizing. Retrieved 2009-04-11. "It is possible to use this property to emulate the behavior of browsers that do not correctly support the CSS box model specification." 
  21. ^ "CSS contents and browser compatibility". quirksmode.org. http://www.quirksmode.org/css/contents.html. 

External links